/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Assignment.BLO;

import Assignment.DAO.BookDAO;
import Assignment.DAO.Book_OrderDAO;
import Assignment.DAO.OrderDAO;
import Assignment.DTO.BookListDTO;
import Assignment.DTO.Book_OrderDTO;
import Assignment.DTO.OrderDTO;
import Assignment.DTO.OrderListDTO;
import Assignment.Util.ConnectionUtil;
import Controller.CartList;
import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

/**
 *
 * @author dieptn
 */
public class OrderBLO {

    private Connection con;

    public String getAllOrders() {
        String path = "";
        try {
            con = ConnectionUtil.getConnection();
            ArrayList<OrderDTO> list = OrderDAO.search(con, null);
            path = ConnectionUtil.getOrderFilePath();
            //if (list.size() > 0) {
            OrderListDTO listOrder = new OrderListDTO();
            for (int i = 0; i < list.size(); i++) {
                listOrder.getOrderList().add(list.get(i));
            }
            JAXBContext ctx = JAXBContext.newInstance(OrderListDTO.class);
            Marshaller mar = ctx.createMarshaller();
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            mar.marshal(listOrder, new File(path));
            //}
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }

    public int insertOrder(OrderDTO order) {
        con = ConnectionUtil.getConnection();
        return OrderDAO.insert(con, order);
    }

    public int updateOrder(OrderDTO order) {
        con = ConnectionUtil.getConnection();
        return OrderDAO.update(con, order);
    }

    public void deleteOrderByOrderID(String orderID) {
        try {
            con = ConnectionUtil.getConnection();
            //return OrderDAO.deleteByOrderID(con, orderID);
            Book_OrderDAO.delete(con, orderID);
            OrderDAO.deleteByOrderID(con, orderID);
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(OrderBLO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int deleteOrderByUsername(String username) {
        con = ConnectionUtil.getConnection();
        return OrderDAO.deleteByUsername(con, username);
    }

    public boolean checkout(String username, Date deliveryDate, float totalPrice, String note, CartList list) {
        con = ConnectionUtil.getConnection();
        Date orderDate = new Date();
        String orderID = String.valueOf(orderDate.getTime());
        Calendar calendar = Calendar.getInstance();
        orderDate = calendar.getTime();
        String status = "Order";
        OrderDTO order = new OrderDTO();
        order.setOrderID(orderID);
        order.setUsername(username);
        order.setOrderDate(orderDate);
        order.setDeliveryDate(deliveryDate);
        order.setTotalPrice(totalPrice);
        order.setNote(note);
        order.setStatus(status);
        OrderDAO.insert(con, order);

        for (int i = 0; i < list.getList().size(); i++) {
            Book_OrderDTO boDTO = new Book_OrderDTO();
            boDTO.setOrderID(orderID);
            boDTO.setBookID(list.getList().get(i).getBookID());
            boDTO.setQuantity(list.getList().get(i).getQuantity());
            Book_OrderDAO.insert(con, boDTO);
        }
        try {
            con.close();
        } catch (Exception ex) {
            Logger.getLogger(OrderBLO.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return true;
    }
}
